package com.tianma315.admin.roottrace.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.tianma315.admin.agent.service.AgentService;
import com.tianma315.admin.check.service.CheckRecordService;
import com.tianma315.admin.check.service.KeepSampleService;
import com.tianma315.admin.check.vo.CheckRecordArrtVO;
import com.tianma315.admin.check.vo.KeepSampleVO;
import com.tianma315.admin.common.exception.MessageException;
import com.tianma315.admin.invocie.domain.vo.InvoiceVo;
import com.tianma315.admin.invocie.service.InvoiceService;
import com.tianma315.admin.material.VO.PurchaseAndCheckAttrVO;
import com.tianma315.admin.material.VO.PurchaseVO;
import com.tianma315.admin.material.domain.PurchaseDO;
import com.tianma315.admin.material.service.MaterialService;
import com.tianma315.admin.material.service.PurchaseService;
import com.tianma315.admin.produce.VO.ProduceVO;
import com.tianma315.admin.produce.VO.ScAdditiveVO;
import com.tianma315.admin.produce.VO.ScBatFeeVO;
import com.tianma315.admin.produce.dao.AdditiveMaterialDao;
import com.tianma315.admin.produce.dao.BatfeeMaterialDao;
import com.tianma315.admin.produce.dao.ScAdditiveDao;
import com.tianma315.admin.produce.dao.ScBatFeeDao;
import com.tianma315.admin.produce.domain.*;
import com.tianma315.admin.produce.service.ProduceTaskService;
import com.tianma315.admin.produce.service.ScAdditiveService;
import com.tianma315.admin.produce.service.ScBatFeeService;
import com.tianma315.admin.product.domain.Product;
import com.tianma315.admin.product.service.ProductService;
import com.tianma315.admin.qa.domain.QaPatrolDO;
import com.tianma315.admin.qa.service.QaCheckService;
import com.tianma315.admin.qa.service.QaPatrolService;
import com.tianma315.admin.qa.service.QaSpotCheckService;
import com.tianma315.admin.qa.vo.QaCheckVO;
import com.tianma315.admin.qa.vo.QaPatrolVO;
import com.tianma315.admin.qa.vo.QaSpotCheckVO;
import com.tianma315.admin.roottrace.service.RootTraceService;
import com.tianma315.admin.stock.domain.vo.WarehousingRecordVo;
import com.tianma315.admin.stock.service.StockService;
import com.tianma315.admin.supplier.service.SupplierService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RootTraceServiceImpl  implements RootTraceService {
    @Autowired
    private StockService stockService;
    @Autowired
    private ProductService productService;
    @Autowired
    private MaterialService materialService;
    @Autowired
    private AgentService agentService;
    @Autowired
    private ProduceTaskService produceTaskService;
    @Autowired
    private InvoiceService invoiceService;
    @Autowired
    private QaPatrolService qaPatrolService;
    @Autowired
    private QaSpotCheckService qaSpotCheckService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private CheckRecordService checkRecordService;
    @Autowired
    private ScBatFeeService scBatFeeService;
    @Autowired
    private BatfeeMaterialDao batfeeMaterialDao;
    @Autowired
    private ScAdditiveService scAdditiveService;
    @Autowired
    private AdditiveMaterialDao additiveMaterialDao;
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private ScAdditiveDao scAdditiveDao;
    @Autowired
    private ScBatFeeDao scBatFeeDao;

    @Autowired
    private QaCheckService qaCheckService;

    @Autowired
    private KeepSampleService keepSampleService;

    @Override
    public JSONObject getTraceInfo(String batch, String productName) {
        JSONObject jsonObject = new JSONObject();

        List<WarehousingRecordVo> result = stockService.getWarehousingRecordByBatchAndProductName(batch, productName);
        if (CollectionUtils.isEmpty(result)){
            throw new MessageException("没有找到记录");
        }else{
            WarehousingRecordVo warehousingRecordVo = result.get(0);
            Product product = productService.getById(warehousingRecordVo.getProductId());


            JSONObject produceInfo = new JSONObject();
            ProduceVO produceVO = produceTaskService.getProduceByProductId(batch,product.getProductId());
            List<ScAdditiveVO> scAdditiveDOS  = scAdditiveDao.getScAdditiveVOPage(produceVO.getProduceTaskId());
            List<ScBatFeeVO> scBatFeeDOS = scBatFeeDao.getScBatFeePage(produceVO.getProduceTaskId());

            List<Integer> acAdditiveIds = scAdditiveDOS.stream().map(item -> item.getAdditiveId()).collect(Collectors.toList());
            List<AdditiveMaterialDO> additiveMaterialDOS = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(acAdditiveIds)){
                Wrapper<AdditiveMaterialDO> wrapper = new EntityWrapper();
//              wrapper.eq("state",0);
                wrapper.in("additive_id",acAdditiveIds);
                additiveMaterialDOS = additiveMaterialDao.selectList(wrapper);
            }


            List<Integer> scBatFeeIds = scBatFeeDOS.stream().map(item -> item.getBatFeeId()).collect(Collectors.toList());
            List<BatfeeMaterialDO> batfeeMaterialDOS = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(scBatFeeIds)){
                Wrapper<BatfeeMaterialDO> batFeeWrapper = new EntityWrapper();
//             batFeeWrapper.eq("state",0);
                batFeeWrapper.in("bat_fee_id",scBatFeeIds);
                batfeeMaterialDOS = batfeeMaterialDao.selectList(batFeeWrapper);
            }


            produceInfo.put("marshallingName",produceVO.getMarshallingName());
            produceInfo.put("additiveInfo",scAdditiveDOS);
            produceInfo.put("scBatFeeDOS",scBatFeeDOS);

            List mapList = new ArrayList();
            for (int i = 0; i < additiveMaterialDOS.size(); i++){
                Map map = new HashMap();
                map.put("id",additiveMaterialDOS.get(i).getMaterialId());
                map.put("batch",additiveMaterialDOS.get(i).getBatch());
                mapList.add(map);
            }

            for (int i = 0; i < batfeeMaterialDOS.size(); i++){
                Map map = new HashMap();
                map.put("id",batfeeMaterialDOS.get(i).getMaterialId());
                map.put("batch",batfeeMaterialDOS.get(i).getBatch());
                mapList.add(map);
            }

            List<PurchaseAndCheckAttrVO> purchaseDOS = new ArrayList<>();
            for (int i = 0; i < mapList.size(); i++) {
                Map map = (Map) mapList.get(i);
                Integer id = Integer.parseInt(map.get("id").toString());
                String purchaseBatch = map.get("batch").toString();
                List<PurchaseAndCheckAttrVO> purchaseList = purchaseService.getPurchaseByBatchAndMaterialId(id,purchaseBatch);
                purchaseDOS.addAll(purchaseList);
            }

            List<InvoiceVo> invoiceList = invoiceService.getInvoiceByProductId(product.getProductId(), batch);
            List<QaPatrolVO> qaPatrolVOList = qaPatrolService.getQaPatroByBatch(batch);
            List<QaCheckVO> qaCheckVOList = qaCheckService.getQaCheckByBatch(batch);
            //产品检测
            CheckRecordArrtVO checkRecordAndArrtVO = checkRecordService.getCheckRecordArrtVOWarehousingRecordId2(Long.valueOf(warehousingRecordVo.getWarehousingRecordId()).intValue());

            //留样管理
            List<KeepSampleVO> keepSampleVOList = keepSampleService.getKeepSampleByBatchAndProductId(batch,product.getProductId());

//            invoiceList.stream().map(item -> item.getProducts().get(0).getNumber()).

            Integer sumProduct= 0;
            for (InvoiceVo item:invoiceList) {
//                for (int j = 0; j < item.getProducts().size(); j++) {
//                    sumProduct+=item.getProducts().get(j).getProductSize();
//                }
                sumProduct += item.getSumPro();
            }

            JSONObject checkInfo = new JSONObject();
            checkInfo.put("purchaseCheck",purchaseDOS);
            checkInfo.put("productCheck",checkRecordAndArrtVO);

            jsonObject.put("productName",warehousingRecordVo.getProductName());
            jsonObject.put("batch",batch);
            jsonObject.put("productInfo",product);
            jsonObject.put("materialAndSupplierInfo",purchaseDOS);
            jsonObject.put("produceInfo",produceInfo);
            jsonObject.put("produceDateInfo",warehousingRecordVo.getProduceDate());
            jsonObject.put("invoiceInfo",invoiceList);
            jsonObject.put("checkInfo",checkInfo);
            jsonObject.put("qaPatrol",qaPatrolVOList);
            jsonObject.put("qaCheck",qaCheckVOList);
            jsonObject.put("warehousingRecord",warehousingRecordVo);
            jsonObject.put("keepSample",keepSampleVOList);
            jsonObject.put("sumProduct",sumProduct);
        }
        return jsonObject;
    }
}
